﻿namespace Trading.Studio

open System
open System.Windows

module App = 

    type DateTime with
        member this.IsWeekEnd = this.DayOfWeek = DayOfWeek.Saturday || this.DayOfWeek = DayOfWeek.Sunday
        member this.Between(start, till) = this > start && this < till 
        member this.NotBetween = not << this.Between

    let marketOpenPeriod = DateTime.Parse("9:30"), DateTime.Parse("16:00")
    let marketIsClosed() = 
        let now = DateTime.Now
        now.IsWeekEnd || now.NotBetween marketOpenPeriod

    let configure() =
        let simulationRequested = Environment.GetCommandLineArgs() |> Array.exists "/simulation".Equals
        if marketIsClosed() || simulationRequested then
            MainWindow.Controller.Create <- fun() ->
                MainWindow.Controller(
                    model = MainWindowViewModel.Create(),
                    brokerGateway = MarketDataTest.mock())

    [<STAThread>]
    [<EntryPoint>]
    let main _ = 
        configure()
        let mainView = new MainWindow.View()
        let controller = MainWindow.Controller.Create()
        controller.Activate(mainView)
        Application().Run mainView.UnderlyingWpfWindow

